﻿using Chat_IService;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Chat_Common;
using SqlSugar.Extensions;

namespace Chat_Service
{
    /***这里面写的代码不会给覆盖,如果要重新生成请删除 Repository.cs ***/

    /// <summary>
    /// 仓储模式
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseService<T> : SimpleClient<T>, IBaseService<T> where T : class, new()
    {

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context"></param>
        public BaseService(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
        {
            if (context == null)
            {
                base.Context = Db;
            }
        }

        /// <summary>
        /// SqlSugarScope操作数据库是线程安的可以单例
        /// </summary>
        public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
        {
            DbType = SqlSugar.DbType.MySql,
            //ConnectionString = @"server=localhost;Database=chat;Uid=root;Pwd=",
            ConnectionString =Appsettings.app(new string[] { "DBLink" }),
            IsAutoCloseConnection = true
        },
         db =>
         {
             //如果用单例配置要统一写在这儿
             db.Aop.OnLogExecuting = (s, p) =>
             {

             };
         });
        /// <summary>
        /// 更新
        /// </summary>
        public bool Update(Expression<Func<T, bool>> columns, Expression<Func<T, bool>> whereExpression)
        {
            return Context.Updateable<T>()
                 .SetColumns(columns)
                .Where(whereExpression).ExecuteCommand() > 0;
        }

        //public SimpleClient<ChangeType> Change<ChangeType>() where ChangeType : class, new()
        //{
        //    return Context.GetSimpleClient<ChangeType>();
        //}

        //public RepositoryType ChangeRepository<RepositoryType>() where RepositoryType : ISugarRepository
        //{
        //    Type typeFromHandle = typeof(RepositoryType);
        //    object[] args = new string[1];
        //    object obj = Activator.CreateInstance(typeFromHandle, args);
        //    RepositoryType result = (RepositoryType)obj;
        //    if (result.Context == null)
        //    {
        //        result.Context = Context;
        //    }

        //    return result;
        //}
        //public override T GetById(dynamic id)
        //{
        //    return Context.Queryable<T>().InSingle(id);
        //}

        //public override List<T> GetList()
        //{
        //    return Context.Queryable<T>().ToList();
        //}

        //public override List<T> GetList(Expression<Func<T, bool>> whereExpression)
        //{
        //    return Context.Queryable<T>().Where(whereExpression).ToList();
        //}

        //public override T GetSingle(Expression<Func<T, bool>> whereExpression)
        //{
        //    return Context.Queryable<T>().Single(whereExpression);
        //}

        //public T GetFirst(Expression<Func<T, bool>> whereExpression)
        //{
        //    return Context.Queryable<T>().First(whereExpression);
        //}

        //public override List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel page)
        //{
        //    int totalNumber = 0;
        //    List<T> result = Context.Queryable<T>().Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref totalNumber);
        //    page.TotalCount = totalNumber;
        //    return result;
        //}

        //public override List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
        //{
        //    int totalNumber = 0;
        //    List<T> result = Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression)
        //        .ToPageList(page.PageIndex, page.PageSize, ref totalNumber);
        //    page.TotalCount = totalNumber;
        //    return result;
        //}

        //public override List<T> GetPageList(List<IConditionalModel> conditionalList, PageModel page)
        //{
        //    int totalNumber = 0;
        //    List<T> result = Context.Queryable<T>().Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref totalNumber);
        //    page.TotalCount = totalNumber;
        //    return result;
        //}

        //public override List<T> GetPageList(List<IConditionalModel> conditionalList, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
        //{
        //    int totalNumber = 0;
        //    List<T> result = Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(conditionalList)
        //        .ToPageList(page.PageIndex, page.PageSize, ref totalNumber);
        //    page.TotalCount = totalNumber;
        //    return result;
        //}

        //public override bool IsAny(Expression<Func<T, bool>> whereExpression)
        //{
        //    return Context.Queryable<T>().Where(whereExpression).Any();
        //}

        //public override int Count(Expression<Func<T, bool>> whereExpression)
        //{
        //    return Context.Queryable<T>().Where(whereExpression).Count();
        //}

        //public override bool Insert(T insertObj)
        //{
        //    return Context.Insertable(insertObj).ExecuteCommand() > 0;
        //}

        //public override int InsertReturnIdentity(T insertObj)
        //{
        //    return Context.Insertable(insertObj).ExecuteReturnIdentity();
        //}

        //public override long InsertReturnBigIdentity(T insertObj)
        //{
        //    return Context.Insertable(insertObj).ExecuteReturnBigIdentity();
        //}

        //public override long InsertReturnSnowflakeId(T insertObj)
        //{
        //    return Context.Insertable(insertObj).ExecuteReturnSnowflakeId();
        //}

        //public override List<long> InsertReturnSnowflakeId(List<T> insertObjs)
        //{
        //    return Context.Insertable(insertObjs).ExecuteReturnSnowflakeIdList();
        //}

        //public override Task<long> InsertReturnSnowflakeIdAsync(T insertObj)
        //{
        //    return Context.Insertable(insertObj).ExecuteReturnSnowflakeIdAsync();
        //}

        //public override Task<List<long>> InsertReturnSnowflakeIdAsync(List<T> insertObjs)
        //{
        //    return Context.Insertable(insertObjs).ExecuteReturnSnowflakeIdListAsync();
        //}

        //public override bool InsertRange(T[] insertObjs)
        //{
        //    return Context.Insertable(insertObjs).ExecuteCommand() > 0;
        //}

        //public override bool InsertRange(List<T> insertObjs)
        //{
        //    return Context.Insertable(insertObjs).ExecuteCommand() > 0;
        //}

        //public override bool Update(T updateObj)
        //{
        //    return Context.Updateable(updateObj).ExecuteCommand() > 0;
        //}

        //public override bool UpdateRange(T[] updateObjs)
        //{
        //    return Context.Updateable(updateObjs).ExecuteCommand() > 0;
        //}

        //public override bool UpdateRange(List<T> updateObjs)
        //{
        //    return Context.Updateable(updateObjs).ExecuteCommand() > 0;
        //}

        //public override bool Update(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression)
        //{
        //    return Context.Updateable<T>().SetColumns(columns).Where(whereExpression)
        //        .ExecuteCommand() > 0;
        //}

        //public override bool Delete(T deleteObj)
        //{
        //    return Context.Deleteable<T>().Where(deleteObj).ExecuteCommand() > 0;
        //}

        //public override bool Delete(Expression<Func<T, bool>> whereExpression)
        //{
        //    return Context.Deleteable<T>().Where(whereExpression).ExecuteCommand() > 0;
        //}

        //public override bool DeleteById(dynamic id)
        //{
        //    return Context.Deleteable<T>().In(id).ExecuteCommand() > 0;
        //}

        //public override bool DeleteByIds(dynamic[] ids)
        //{
        //    return Context.Deleteable<T>().In(ids).ExecuteCommand() > 0;
        //}

        //public override Task<T> GetByIdAsync(dynamic id)
        //{
        //    return Context.Queryable<T>().InSingleAsync(id);
        //}

        //public override Task<List<T>> GetListAsync()
        //{
        //    return Context.Queryable<T>().ToListAsync();
        //}

        //public override Task<List<T>> GetListAsync(Expression<Func<T, bool>> whereExpression)
        //{
        //    return Context.Queryable<T>().Where(whereExpression).ToListAsync();
        //}

        //public async override Task<T> GetSingleAsync(Expression<Func<T, bool>> whereExpression)
        //{
        //    return await base.GetSingleAsync(whereExpression);
        //}

        //public async Task<T> GetFirstAsync(Expression<Func<T, bool>> whereExpression)
        //{
        //    return await base.GetFirstAsync(whereExpression);
        //}

        //public override Task<bool> IsAnyAsync(Expression<Func<T, bool>> whereExpression)
        //{
        //    return base.IsAnyAsync(whereExpression);
        //}
        //public override Task<int> CountAsync(Expression<Func<T, bool>> whereExpression)
        //{
        //    return base.CountAsync(whereExpression);
        //}
        //public override Task<bool> InsertAsync(T insertObj)
        //{
        //    return base.InsertAsync(insertObj);
        //}
        //public override Task<int> InsertReturnIdentityAsync(T insertObj)
        //{
        //    return base.InsertReturnIdentityAsync(insertObj);
        //}
        //public override Task<long> InsertReturnBigIdentityAsync(T insertObj)
        //{
        //    return base.InsertReturnBigIdentityAsync(insertObj);
        //}
        //public override Task<bool> InsertRangeAsync(T[] insertObjs)
        //{
        //    return base.InsertRangeAsync(insertObjs);
        //}
        //public override Task<bool> InsertRangeAsync(List<T> insertObjs)
        //{
        //    return base.InsertRangeAsync(insertObjs);
        //}
        //public override Task<bool> UpdateAsync(T insertObj)
        //{
        //    return base.UpdateAsync(insertObj);
        //}
        //public override Task<bool> UpdateRangeAsync(T[] updateObjs)
        //{
        //    return base.UpdateRangeAsync(updateObjs);
        //}
        //public override Task<bool> UpdateRangeAsync(List<T> updateObjs)
        //{
        //    return base.UpdateRangeAsync(updateObjs);
        //}
        //public override Task<bool> UpdateAsync(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression)
        //{
        //    return base.UpdateAsync(columns, whereExpression);
        //}
        //public override Task<bool> DeleteAsync(T insertObj)
        //{
        //    return base.DeleteAsync(insertObj);
        //}

        //public override Task<bool> DeleteAsync(Expression<Func<T, bool>> whereExpression)
        //{
        //    return base.DeleteAsync(whereExpression);
        //}

        ////public override Task<bool> DeleteByIdAsync(dynamic id)
        ////{
        ////    return base.DeleteByIdAsync(id);
        ////}
        //public override Task<bool> DeleteByIdsAsync(dynamic[] ids)
        //{
        //    return base.DeleteByIdsAsync(ids);
        //}
    }
}